/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ejb;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import jpa.ApprovazioneDocumento;
import jpa.Documento;
import jpa.Studente;
import jpa.Tutor;
import jpa.Upload;
import jpa.UploadTemp;

@Stateless
public class GestioneFile implements GestioneFileLocal {

    @PersistenceContext
    private EntityManager em;

    public List<Documento> listaFile(int start, int end) {
        Query query = em.createNamedQuery("Documento.findAll");
        query = query.setFirstResult(start);
        query = query.setMaxResults(end);
        List<Documento>result = query.getResultList();
        return result;
    }

    public List<UploadTemp> listaFileDaApprovare(int start, int end) {
        Query query = em.createNamedQuery("UploadTemp.findAll");
        query = query.setFirstResult(start);
        query = query.setMaxResults(end);
        List<UploadTemp>result = query.getResultList();
        return result;
    }

    public boolean uploadStudente(UploadTemp upload) {
        em.persist(upload);
        em.flush();
        return true;
    }

    public boolean uploadTutor(Upload up, ApprovazioneDocumento ad, Documento doc) {
        Upload u = up;
        ApprovazioneDocumento a = ad;
        Documento d = doc;
        em.persist(u);
        em.flush();
        d.setUploadid(u.getId());
        em.persist(a);
        em.flush();
        d.setApprovazioneDocumentoid(a.getId());
        em.persist(d);
        em.flush();
        return true;

    }

    public void approvaFile(int id, int tutorId) {
        UploadTemp tmp = em.find(UploadTemp.class, id);
        Upload up = new Upload();
        up.setData(tmp.getData());
        up.setStudenteid(tmp.getStudenteid());
        ApprovazioneDocumento ad = new ApprovazioneDocumento();
        ad.setData(new Date());
        ad.setTutorid(tutorId);
        Documento d = new Documento();
        d.setCorsiid(tmp.getCorsoid());
        d.setDescr(tmp.getDescr());
        d.setLink(tmp.getLink());
        d.setTitolo(tmp.getTitolo());
        em.persist(up);
        em.flush();
        d.setUploadid(up.getId());
        em.persist(ad);
        em.flush();
        d.setApprovazioneDocumentoid(ad.getId());
        em.persist(d);
        em.remove(tmp);
        em.flush();
    }

    public void eliminaFileDaApprovare(int id) {
        UploadTemp tmp = em.find(UploadTemp.class, id);
        em.remove(tmp);
    }

    public List<Documento> ricerca(String termine, String cosa) {
        List<Documento> result = new LinkedList<>();
        if(cosa.equals("titolo")) {
            result = ricercaPerTitolo(termine);
        }
        if(cosa.equals("descrizione")) {
            result = ricercaPerDescrizione(termine);
        }
        if(cosa.equals("autore")) {
            result = ricercaPerAutore(termine);
        }
        return result;
    }

    private List<Documento> ricercaPerAutore(String termine){
        List<Documento> result = new LinkedList<>();
        String stud = "Select s From Studente s Where s.cognome  like '%" + termine + "%'";
        Query queryStud = em.createQuery(stud);
        List<Studente> studente = queryStud.getResultList();
        Iterator<Studente> its = studente.iterator();
        while(its.hasNext()){
            int id = its.next().getId();
            String queryUp = "Select u From Upload u Where u.studenteid = " + id;
            Query qUp = em.createQuery(queryUp);
            List<Upload> lUp = qUp.getResultList();
            Iterator<Upload> itU = lUp.iterator();
            while(itU.hasNext()){
                int idUp = itU.next().getId();
                Query qd = em.createNamedQuery("Documento.findByUploadid");
                qd.setParameter("uploadid", idUp);
                result.add((Documento) qd.getSingleResult());
            }
        }
        String tutor = "Select t From Tutor t Where t.cognome like '%" + termine + "%'";
        Query queryTutor = em.createQuery(tutor);
        List<Tutor> tut = queryTutor.getResultList();
        Iterator<Tutor> itT = tut.iterator();
        while(itT.hasNext()){
            int id = itT.next().getId();
            String queryUp = "Select u From Upload u Where u.tutorid = " + id;
            Query qUp = em.createQuery(queryUp);
            List<Upload> lUp = qUp.getResultList();
            Iterator<Upload> itU = lUp.iterator();
            while(itU.hasNext()){
                int idUp = itU.next().getId();
                Query qd = em.createNamedQuery("Documento.findByUploadid");
                qd.setParameter("uploadid", idUp);
                result.add((Documento) qd.getSingleResult());
            }
        }
        return result;
    }
    
    private List<Documento> ricercaPerDescrizione(String termine){
        List<Documento> result = new LinkedList<>();
        String q = "Select d From Documento d Where d.descr like '%" + termine + "%'";
        Query query = em.createQuery(q);
        result = query.getResultList();
        return result;
    }

    private List<Documento> ricercaPerTitolo(String termine){
        List<Documento> result = new LinkedList<>();
        String q = "Select d From Documento d Where d.titolo like '%" + termine + "%'";
        Query query = em.createQuery(q);
        result = query.getResultList();
        return result;
    }

    public int numeroFile() {
        Long result = (Long) em.createNamedQuery("Documento.countFile").getSingleResult();
        return result.intValue();
    }

    public int numeroFileApprovare() {
        Long result = (Long) em.createNamedQuery("UploadTemp.countFile").getSingleResult();
        return result.intValue();
    }


}
